﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DXApplicationTangche.UC.门店出入库.Service;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.service;
using DXApplicationTangche.DTO;
using DXApplicationTangche.Utils;

namespace DXApplicationTangche.UC.门店出入库.淘宝
{
    public partial class Form淘宝发运 : DevExpress.XtraEditors.XtraForm
    {
        private int size = 3;
        private List<GarmentPackageInfoDTO> garmentPackageInfoDTO未发运 = new List<GarmentPackageInfoDTO>();//未发运一览
        private List<GarmentPackageInfoDTO> garmentPackageInfoDTOs已发运 = new List<GarmentPackageInfoDTO>();//已发运一览
        private GarmentPackageInfoDTO garmentPackageInfoDTO = new GarmentPackageInfoDTO();//包装信息
        public Form淘宝发运()
        {
            InitializeComponent();
        }

        private void simpleButton未发运_Click(object sender, EventArgs e)
        {
            this.刷新未发运();
        }
        private void 刷新未发运()
        {
            this.garmentPackageInfoDTO未发运 = 出入库Service.get未发运成衣包装一览();
            this.gridControl未发运.DataSource = this.garmentPackageInfoDTO未发运;
            this.gridView未发运.RefreshData();
        }

        private void simpleButton已发运_Click(object sender, EventArgs e)
        {
            this.garmentPackageInfoDTOs已发运 = 出入库Service.get已发运成衣包装一览();
            this.gridControl已发运.DataSource = this.garmentPackageInfoDTOs已发运;
            this.gridView已发运.RefreshData();
        }

        private void 顺丰发运ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String packageBarcode = FunctionHelper.GetgridViewValue(
                                        this.gridView未发运,
                                        this.gridView未发运.FocusedRowHandle,
                                        "packageBarcode"
                                        );
            if (this.checkBox是否两联.Checked)
            {
                this.size = 2;
            }
            if (!String.IsNullOrEmpty(packageBarcode))
            {
                try
                {
                    DTO发运 dTO发运 = 出入库Service.get成衣顺丰打印订单(packageBarcode,size);
                    if ("200".Equals(dTO发运.errorCode))
                    {
                        FileUtil.DownloadPicture(URL.图片url + "/sfexpress/" + packageBarcode + ".jpg", @"pic\" + packageBarcode + ".jpg", -1);
                        System.Diagnostics.Process.Start(@"pic\" + packageBarcode + ".jpg");
                    }
                    else
                    {
                        MessageBox.Show(dTO发运.errorMessage);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else
            {
                MessageBox.Show("选择要发运的数据");
            }
            this.刷新未发运();
        }

        private void simpleButton手机发货_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.textBox包装码.Text.Trim()))
            {
                MessageBox.Show("填写发货包装码");
                return;
            }
            if (String.IsNullOrWhiteSpace(this.textBox快递单号.Text.Trim()))
            {
                MessageBox.Show("填写发货快递单号");
                return;
            }
            try
            {
                this.garmentPackageInfoDTO = 出入库Service.get包装信息(this.textBox包装码.Text.Trim());
                if (!String.IsNullOrEmpty(this.garmentPackageInfoDTO.packageBarcode))
                {
                    new Form包装信息(this.garmentPackageInfoDTO, this.textBox快递单号.Text.Trim()).ShowDialog();
                    this.刷新未发运();
                }
                else
                {
                    MessageBox.Show("无包装信息");
                }
            }
            catch
            {
                MessageBox.Show("无包装信息");
            }
            this.textBox包装码.Clear();
            this.textBox快递单号.Clear();
            this.ActiveControl = this.textBox包装码;
            this.checkBox自取.Checked = false;
        }

        private void Form淘宝发运_Load(object sender, EventArgs e)
        {
            this.ActiveControl = this.textBox包装码;
        }

        private void textBox包装码_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                this.ActiveControl = this.textBox快递单号;
            }
        }

        private void checkBox自取_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox自取.Checked)
            {
                this.textBox快递单号.Enabled = false;
                this.textBox快递单号.Text = "自取";
            }
            else
            {
                this.textBox快递单号.Enabled = true;
                this.textBox快递单号.Clear();
                this.ActiveControl = this.textBox快递单号;
            }
        }
    }
}